package com.example.tnot.Teacher.CoursePart.Container.AddCourse;

import static android.app.Activity.RESULT_OK;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.tnot.R;
import com.example.tnot.Teacher.BaseFragment.BaseBackFragment;

public class AddCourseFragment extends BaseBackFragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    View view;
    ImageView iv_back;
    LinearLayout ll_select;
    EditText et_CourseName;
    LinearLayout ll_cover;
    Uri selectedImageUri;

    private String mParam1;
    private String mParam2;
    private static final int REQUEST_CODE_PICK_IMAGE = 1;
    TextView tv_finish;

    public AddCourseFragment() {

    }

    public static AddCourseFragment newInstance(String param1, String param2) {
        AddCourseFragment fragment = new AddCourseFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.course_add_fragment, container, false);
        //处理返回逻辑
        iv_back = view.findViewById(R.id.back);
        et_CourseName = view.findViewById(R.id.et_CourseName);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handleBackPressed();
            }
        });
        //学期选择点击监听
        ll_select = view.findViewById(R.id.ll_select);
        ll_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NavController navController = Navigation.findNavController(v);
                navController.navigate(R.id.action_addChapterFragment_to_selectClassFragment);
            }
        });
        //选择封面点击监听
        ll_cover = view.findViewById(R.id.ll_cover);
        ll_cover.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK,
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, REQUEST_CODE_PICK_IMAGE);
            }
        });
        tv_finish = view.findViewById(R.id.tv_finish);
        tv_finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String course_name = et_CourseName.getText().toString();
                Intent ResultIntent = new Intent();
                if (selectedImageUri != null){
                    //将获取到的课程名称传回去
                    ResultIntent.putExtra("course_name", course_name);

                    //将获取到的图片传回去
//                    ResultIntent.putExtra("cover_uri", selectedImageUri.toString());
                }
                //处理回调逻辑

                handleBackPressed();
            }
        });


        return view;
    }
    // 在 AddCourseFragment 类中重写 onActivityResult 方法
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode,resultCode, data);

        if (requestCode == REQUEST_CODE_PICK_IMAGE && resultCode == RESULT_OK && data != null) {
            //获取图片
            selectedImageUri = data.getData();

            // 使用 Glide 加载图片到 ImageView
            ImageView imageView = view.findViewById(R.id.iv_icon);
            Glide.with(this)
                    .load(selectedImageUri)
                    .into(imageView);
        }
    }
}